package com.ioudi.digu.flex.message;

import org.springframework.util.Assert;

import com.ioudi.digu.flex.config.MessageServiceConfigProcessor;

import flex.messaging.Destination;
import flex.messaging.MessageBroker;
import flex.messaging.MessageDestination;
import flex.messaging.services.MessageService;
import flex.messaging.services.Service;

public class MessageDestinationFactory extends
		org.springframework.flex.messaging.MessageDestinationFactory
{
	 @Override
	    protected Destination createDestination(String destinationId, MessageBroker broker) throws Exception {
	        MessageService messageService = (MessageService) broker.getServiceByType(getMessageServiceClassName());
	        Assert.notNull(messageService, "Could not find a proper MessageService in the Flex MessageBroker.");
	        MessageDestination destination = (MessageDestination) messageService.createDestination(destinationId);
	        return destination;
	    }

	    /**
	     * 
	     * {@inheritDoc}
	     */
	    @Override
	    protected void destroyDestination(String destinationId, MessageBroker broker) {
	        MessageService messageService = (MessageService) broker.getServiceByType(getMessageServiceClassName());
	        if (messageService == null) {
	            return;
	        }
	        messageService.removeDestination(destinationId);
	    }

	    /**
	     * 
	     * {@inheritDoc}
	     */
	    @Override
	    protected Service getTargetService(MessageBroker broker) {
	        return broker.getServiceByType(getMessageServiceClassName());
	    }
	    
	    protected String getMessageServiceClassName()
	    {
	    	return MessageServiceConfigProcessor.SERVICE_CLASS_NAME;
	    }
}
